﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Framework;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public abstract class Address : UserControlBase
    {
        private string _Address1;
        private string _Address2;
        private NB_Store_AddressInfo _AddressDataInfo;
        private string _AddressDescription;
        private string _AddressName;
        private string _City;
        private string _CountryCode;
        [AccessedThroughProperty("dlAddress")]
        private DataList _dlAddress;
        private bool _NoValidate;
        private string _PostalCode;
        private string _Phone1;
        private string _Phone2;
        private string _Region;
        private string _TemplateName;

        public Address()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            this._NoValidate = false;
            this._TemplateName = "";
        }

        private NB_Store_AddressInfo GetAddressDataInfo()
        {
            NB_Store_AddressInfo info2 = new NB_Store_AddressInfo();
            return (NB_Store_AddressInfo) SharedFunctions.populateGenObject(this.dlAddress, info2, 0);
        }

        private void Page_Init(object sender, EventArgs e)
        {
            if (this._TemplateName == "")
            {
                this._TemplateName = "checkoutaddress.template";
            }
            string s = SharedFunctions.GetStoreSettingText(this.PortalSettings.PortalId, this._TemplateName, SharedFunctions.GetCurrentCulture());
            if (s != "")
            {
                this.dlAddress.ItemTemplate = new GenXMLTemplate(this.Server.HtmlDecode(s));
            }
            else
            {
                this.dlAddress.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this._AddressDataInfo != null)
            {
                ArrayList list = new ArrayList();
                list.Add(this._AddressDataInfo);
                this.dlAddress.DataSource = list;
                this.dlAddress.DataBind();
            }
            if (!this.Page.IsPostBack && (this.dlAddress.Controls.Count >= 1))
            {
                DropDownList ddlCountry = (DropDownList) this.dlAddress.Controls[0].FindControl("cboCountry");
                if (ddlCountry != null)
                {
                    SharedFunctions.populateCountryList(this.PortalSettings.PortalId, ddlCountry, this._CountryCode);
                }
                if (this._NoValidate)
                {
                    IEnumerator enumerator=null;
                    try
                    {
                        enumerator = this.dlAddress.Controls[0].Controls.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
                            if (objectValue is RequiredFieldValidator)
                            {
                                ((RequiredFieldValidator) objectValue).Enabled = false;
                            }
                            if (objectValue is RegularExpressionValidator)
                            {
                                ((RegularExpressionValidator) objectValue).Enabled = false;
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }
                }
            }
        }

        public NB_Store_AddressInfo AddressDataInfo
        {
            get
            {
                return this.GetAddressDataInfo();
            }
            set
            {
                this._AddressDataInfo = value;
                this._CountryCode = this._AddressDataInfo.CountryCode;
            }
        }

        public string CountryCode
        {
            set
            {
                this._CountryCode = value;
            }
        }

        protected virtual DataList dlAddress
        {
            get
            {
                return this._dlAddress;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._dlAddress = value;
            }
        }

        public bool NoValidate
        {
            get
            {
                return this._NoValidate;
            }
            set
            {
                this._NoValidate = value;
            }
        }

        public string TemplateName
        {
            set
            {
                this._TemplateName = value;
            }
        }
    }
}

